<?php


namespace App\Http\Services\Admin;


use App\Http\Constants\JobsConstants;
use App\Models\FailedJobs;
use YiluTech\ComplexSearch\ComplexSearch;

class FailedJobsService extends ComplexSearch
{
    public $root = FailedJobs::class;

    protected $display = 'simple';
    protected $orderBy = 'id desc';

    protected $headers = [
        ['value' => 'id', 'label' => '序号'],
        ['value' => 'uuid', 'label' => 'uuid'],
        ['value' => 'queue', 'label' => '名称'],
        ['value' => 'payload', 'label' => '数据'],
        ['value' => 'exception', 'label' => '错误信息'],
        ['value' => 'failed_at', 'label' => '创建时间'],
    ];

    protected $conditions = [
        'queue' => [
            'label' => '队列类型',
            'ctype' => 'select',
            'operator' => JobsConstants::QUEUE,
        ],
        'failed_at' => [
            'label' => '创建时间',
            'ctype' => 'date-in',
            'format' => 'Y-m-d',
            'display' => true
        ]
    ];

    public function get()
    {
        $result = parent::get();
        if ($this->action !== 'prepare') {
            $result = $result->toArray();
            $result = CommonService::objRegroupArr($result);
        }
        return $result;

    }

    public function query()
    {
        $query = parent::query();
        if ($this->action != 'prepare') {
            $query->select('id', 'uuid', 'queue', 'payload', 'exception','failed_at');
        }
        return $query;
    }

}
